java AddUpAll < manyNums.dat
Professionally written code carefully checks the input for errors. We won't do that, but will rely on what comes automatically with Java. Say that the input file looked like this:
5 1 2 3 4
The file is in error since it says that five integers are to be added up, but only four integers follow. The line after the "4" is blank. Here is what will happen if you run the program with this file (the user prompts have been left in this code):
C:\users\default\JavaLessons>java AddUpAll < data.dat Enter how many integers: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: java.lang.NumberFormatException: C:\users\default\JavaLessons>
The fifth execution of readLine()
returned an empty string (because the line had nothing in it),
which could not be converted to an integer.
If the very last character in the file was the "4" there
also would have been an exception thrown at run time.
The Java system will let us know if the file has characters
that can't be converted into integers, or is shorter than
is called for.
Here is another defective file:
5 10 20 30 40 50 60 70